package com.mk.service.web;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.*;
import com.mk.domain.ZMembers;
import com.mk.enums.ErrorCode;
import com.mk.exception.UserException;
import com.mk.util.S2;
import com.mk.util.WechatUtils;
import me.hao0.wechat.model.user.UserInfo;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.util.C;
import org.osgl.util.S;

import javax.inject.Inject;
import java.util.Map;

@Controller("login")
@Api(group = "wb", title = "用户", path = "/login")
public class LoginService extends BaseWebService {
    @Inject
    private ZMembers.Mapper zMembersMapper;

    @PostAction("loginByWxCode.json")
    @ApiMethod(title = "客户端微信授权登录", path = "/loginByWxCode.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "authCode", title = "客户端微信授权登录", type = String.class)
    @ApiReturn(description = "web用户信息", type = Map.class, maps = {
            @ApiMap(title = "web用户对象", name = "members", type = ZMembers.class)
    })
    public Map<String, Object> loginByWxCode(String authCode) {
        //默认用户登录
        if(S.eq("cccc",authCode)){
            ZMembers param = new ZMembers();
            param.setOpenid(authCode);
            ZMembers members = zMembersMapper.templateOne(param);
            this.login(members);
            return C.Map("members", members);
        }
        try {
            UserInfo map = WechatUtils.getUserInfoByCode(authCode);
            //根据openidid获取用户信息
            ZMembers members = getUserByOpenid(map.getOpenId());
            if (members == null) {
                members = new ZMembers();
                members.setMid(S2.UUID());
                members.setOpenid(map.getOpenId());
                members.setHeadurl(map.getHeadImgUrl());
                members.setNickname(map.getNickName());
                members.setMdate(DateUtil.now());
                members.setCityname(map.getCity());
                zMembersMapper.insert(members);
            }
            this.login(members);
            return C.Map("members", members);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UserException(ErrorCode.E201002);
        }
    }


    @PostAction("logout.json")
    @ApiMethod(title = "退出方法", path = "/logout.json", method = ApiHttpMethod.POST)
    public void logout2() {
        super.logout();
    }


    @GetAction("getCurUserInfo.json")
    @ApiMethod(title = "获取当前用户", path = "/getCurUserInfo.json", method = ApiHttpMethod.GET)
    @ApiReturn(description = "web用户信息", type = Map.class, maps = {
            @ApiMap(title = "web用户对象", name = "members", type = ZMembers.class)
    })
    public Map<String, Object> getCurUserInfo() {
        ZMembers members = zMembersMapper.single(me.getMid());
        return C.Map("members", members);
    }


    @GetAction("getUserInfoByOpenid.json")
    @ApiMethod(title = "获取当前用户", path = "/getUserInfoByOpenid.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "openid", title = "openid", type = String.class)
    @ApiReturn(description = "web用户信息", type = Map.class, maps = {
            @ApiMap(title = "web用户对象", name = "members", type = ZMembers.class)
    })
    public ZMembers getUserByOpenid(String openid) {
        ZMembers param = new ZMembers();
        param.setOpenid(openid);
        return zMembersMapper.templateOne(param);
    }


}
